<?php
/**
 * 前台共用函数库
*/

/* 打印变量 */
function dump($date){
 echo '<pre>';
 print_r($date);
 echo '</pre>';
}

/**
* 获取月份第一天与最后一天时间戳
* @param int $y      年份
* @param int $m      本月结束时间戳
* @return array
*/
function mFristAndLast($y = "", $m = ""){
    if ($y == "") $y = date("Y");
    if ($m == "") $m = date("m");
    $m = sprintf("%02d", intval($m));
    $y = str_pad(intval($y), 4, "0", STR_PAD_RIGHT);
    $m > 12 || $m < 1 ? $m = 1 : $m = $m;
    $firstday = strtotime($y . $m . "01000000");
    $firstdaystr = date("Y-m-01", $firstday);
    $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
    return array(
        "firstday" => $firstday,
        "lastday" => $lastday,
    );
}

/**
 * 格式化价格
 * @param  float   $price    价格
 * @param  int     $type     格式：0、00.00，1、￥00.00，2、<span class="f">￥</span><span class="m">00</span><span class="i">.00</span>
 */
function priceFormat ($price = 0, $type = 0)
{
    switch ($type)
    {
        case 0:
        $price = number_format($price, 2, '.', '');
        break;
        case 1:
        $price = ￥ . number_format($price, 2, '.', '');
        break;
        case 2:
        $arr = implode('.', number_format($price, 2, '.', ''));
        $price = sprintf('<span class="f">￥</span><span class="m">%s</span><span class="i">.%s</span>', $arr[0], '.' . $arr[1]);
        break;
    }
    return $price;
}

/**
 * 获取地区名称
 */
function getRegionName ($id)
{
    $sql = "SELECT region_name FROM " . T('region') . " WHERE region_id = '$id'";
    return D()->getOne($sql);
}

/**
 * 获取支付方式名称
 */
function getPayName ($id)
{
    switch ($id)
    {
        case 1:
        return '支付宝';
        case 2:
        return '货到付款';
    }
}

/**
 * 获取配送方式名称
 */
function getShippingName ($id)
{
    switch ($id)
    {
        case 1:
        return '申通快递';
        case 2:
        return '中通快递';
    }
}
/**
 * 获取状态名称
 */
function getStatusName ($id)
{
    include_once(FW . 'llib/status.php');
    if (!empty($_LANG['status']["$id"])) {
        return $_LANG['status']["$id"];
    } else {
        return false;
    }

}